package com.gec.hawaste.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.hawaste.entity.SysArea;
import com.gec.hawaste.entity.SysOffice;
import com.gec.hawaste.entity.Waste;
import com.gec.hawaste.entity.WasteType;
import com.gec.hawaste.mapper.SysAreaMapper;
import com.gec.hawaste.mapper.SysOfficeMapper;
import com.gec.hawaste.mapper.WasteMapper;
import com.gec.hawaste.mapper.WasteTypeMapper;
import com.gec.hawaste.service.ISysOfficeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.hawaste.utils.PageInfo;
import com.gec.hawaste.vo.SysOfficeDo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 机构表 服务实现类
 * </p>
 *
 * @author chenxys
 * @since 2022-07-01
 */
@Service
public class SysOfficeServiceImpl extends ServiceImpl<SysOfficeMapper, SysOffice> implements ISysOfficeService {

    @Autowired
    private WasteMapper wasteMapper;
    @Autowired
    private SysAreaMapper areaMapper;
    @Autowired
    private WasteTypeMapper wasteTypeMapper;
    @Override
    public Page selectByCondition(PageInfo<SysOfficeDo> pageInfo) {
        return baseMapper.selectByCondition(pageInfo);
    }

    @Override
    public SysOfficeDo selectByOfficeId(Long officeId) {
        SysOffice sysOffice = baseMapper.selectById(officeId);
        SysOfficeDo sysOfficeDo = new SysOfficeDo();

        Long areaId = sysOffice.getAreaId();
        SysArea area = areaMapper.findByOfficeId(areaId);
        Waste waste = wasteMapper.findByOfficeId(officeId);
        WasteType wasteType = null;
        if ( waste != null){
            Long parentId = waste.getParentId();
            wasteType = wasteTypeMapper.findByWaste(parentId);
        }

        sysOfficeDo.setSysOffice(sysOffice);
        sysOfficeDo.setSysArea(area);
        sysOfficeDo.setWaste(waste);
        sysOfficeDo.setWasteType(wasteType);
        return sysOfficeDo;
    }
}
